﻿CREATE PROCEDURE [dbo].[proc_CallCenter_Getlist_Count]
	(
		@CompanyId int,
		@DepartmentId int,
		@Stext nvarchar(50)
	)
AS
begin
	Declare  @indextable table(id int identity(1,1),nid int)
		;WITH ChildDepartment(ID,DName,PId) AS
		(
			SELECT ID,DName,ParentID FROM department WHERE id=@departmentid
		    
			UNION ALL
			SELECT department.ID,department.DName,department.ParentID FROM ChildDepartment AS d
			INNER JOIN department ON d.ID = department.ParentID
		)
		Insert into @indextable(nid) select Id from ChildDepartment;
	
	Select Count(Id) From CallCenter
	Where (@CompanyId=0 Or CompanyId=@CompanyId) and (@DepartmentId=0 Or DepartmentId in (select c.nid from @indextable c))
		and (@Stext='' Or EName like '%'+@Stext+'%' Or cast(FJNum as nchar(10)) like '%'+@Stext+'%')
end
	RETURN
